package com.dlkw.easyroom;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.RawQuery;
import androidx.room.Update;
import androidx.sqlite.db.SimpleSQLiteQuery;
import androidx.sqlite.db.SupportSQLiteQuery;

import java.util.List;

@Dao
public interface BaseDao<T> {
    @Insert
    void insert(T... t);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertOrUpdate(T... t);

    @Update
    void update(T... t);

    @Delete
    void delete(T... t);

    @RawQuery
    Object execute(SupportSQLiteQuery query);

    @RawQuery
    List<T> query(SupportSQLiteQuery query);

    default List<T> query(String sql) {
        return this.query(new SimpleSQLiteQuery(sql));
    }

    default List<T> listAll() {
        return this.query(new SimpleSQLiteQuery("select * from " + EasyRoom.getTableName(this)));
    }

    default void deleteAll() {
        this.execute("delete from " + EasyRoom.getTableName(this), Void.class);
    }

    default <O> O execute(String sql, Class<O> cls) {
        return (O) this.execute(new SimpleSQLiteQuery(sql));
    }

    default String getTableName() {
        return EasyRoom.getTableName(this);
    }

    default QueryBuilder<T> queryBuilder() {
        return QueryBuilder.create(this);
    }
}
